//
//  AppreciationViewController.m
//  Hand shadow
//
//  Created by A-01 on 2020/7/21.
//  Copyright © 2020 A-01. All rights reserved.
//

#import "AppreciationViewController.h"
#import "AppreciationCollectionViewCell.h"
#import "ZRImageBrower.h"

@interface AppreciationViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UIImageView * oneImageView;
@property (nonatomic, strong)UIImageView * twoImageView;
@property (nonatomic, strong)UIImageView * threeImageView;
@property (nonatomic, strong)UIImageView * fourImageView;
@property (nonatomic, weak)UICollectionView * collectionView;
@property (nonatomic, strong)NSMutableArray * pictureArr;

@end

@implementation AppreciationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleLabel.text = @"Appreciation";
    [self setupUI];
}

- (void)setupUI{
    
    self.oneImageView = [self setupImageViewWithImg:@"works_1"];
    [self.view addSubview:self.oneImageView];
    self.twoImageView = [self setupImageViewWithImg:@"works_2"];
    [self.view addSubview:self.twoImageView];
    self.threeImageView = [self setupImageViewWithImg:@"works_3"];
    [self.view addSubview:self.threeImageView];
    self.fourImageView = [self setupImageViewWithImg:@"works_4"];
    [self.view addSubview:self.fourImageView];
    
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
    layout.itemSize =CGSizeMake(135, 135);
    layout.minimumLineSpacing = 10;
//    layout.minimumInteritemSpacing = 20;
    //设置collectionView滚动方向
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        //2.初始化collectionView
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(85, Height_StatusBar + 170 , kScreenW - 170, 135) collectionViewLayout:layout];
        
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.showsVerticalScrollIndicator = NO;
        //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
//    [collectionView registerClass:[PictureCollectionViewCell class] forCellWithReuseIdentifier:@"PictureCollectionViewCell"];
    [collectionView registerNib:[UINib nibWithNibName:@"AppreciationCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"AppreciationCollectionViewCell"];
        //4.设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    self.collectionView = collectionView;
    
    [self.view addSubview:self.collectionView];
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    AppreciationCollectionViewCell *cell = (AppreciationCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    [ZRImageBrower showImage:cell.iconImageView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.pictureArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    AppreciationCollectionViewCell *cell = (AppreciationCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"AppreciationCollectionViewCell" forIndexPath:indexPath];
    cell.iconImageView.image = [UIImage imageNamed:self.pictureArr[indexPath.row]];
    return cell;
}


- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];

    [self.threeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(kScreenW / 2 + 25);
        make.top.mas_equalTo(self.backButton.mas_bottom).offset(70);
        make.width.mas_equalTo(45);
        make.height.mas_equalTo(40);
    }];
    
    [self.twoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.threeImageView.mas_leading).offset(-90);
        make.centerY.width.height.mas_equalTo(self.threeImageView);
    }];
    
    [self.oneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.twoImageView.mas_leading).offset(-90);
        make.centerY.width.height.mas_equalTo(self.twoImageView);
    }];
    
    [self.fourImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.threeImageView.mas_trailing).offset(90);
        make.centerY.width.height.mas_equalTo(self.threeImageView);
    }];
}


- (UIImageView *)setupImageViewWithImg:(NSString *)img{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:img];
    
    return imageView;
}


- (NSMutableArray *)pictureArr{
    
    if (!_pictureArr) {
        
        _pictureArr = [NSMutableArray arrayWithCapacity:13];
        for (int i = 1; i < 14; i++) {
            
            NSString * res= [NSString stringWithFormat:@"enjoy_%02d",i];
            [_pictureArr addObject:res];
        }
    }
    
    return _pictureArr;
}

@end
